<?php
/**
 * Created by PhpStorm.
 * User: baich
 * Date: 2018/10/10
 * Time: 11:11
 */
namespace app\logic;

use app\common\HttpCurl;
use app\models\Category;
use app\models\Goods;
use app\models\GoodsCategory;
use app\models\GoodsSku;

class GoodsLogic
{
    public function addGoods($goodstemp,$sku_stock=[]){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $goods = new Goods();
        $goods->yx_goods_id = $goodstemp['id'];
        $goods->goods_name = $goodstemp['name'];
        $time = date('Y-m-d H:i:s');
        $goods->add_time = $time;
        $goods->update_time = $time;
        $goods->simple_desc = $goodstemp['simpleDesc'];
        $goods->attr_list = json_encode($goodstemp['attrList']);
        $goods->goods_detail = $goodstemp['itemDetail']['detailHtml'];
        $goods->goods_pic = HttpCurl::download($goodstemp['listPicUrl']);
        $goods->goods_sort = 0;
        $hot = [3380026,1622005,1622005,1263004,1263004,1634013,1149043,3378002,1686012,1686012,1686012,1686012,1622006,1622006,1156021,1156021,1434006,1459004,1459004,1459004,1459004,1587014,1587014,1553040,1327000,1327000,1327000,1327000,1401005,1401005,1127007,1127007,1127007,1127007,1045000,1076019,1006082,1006082,1006082,1621025,1621025,1085019,1085019,1085019,1085019,1085019,1085019,1085019,1085019,1085019,1601000,1505029];
        if(in_array($goodstemp['id'],$hot))
            $goods->is_hot = 1;
        else
            $goods->is_hot = 0;
        if(HttpCurl::download($goodstemp['itemDetail']['picUrl1'])){
            $goods->goods_pics = HttpCurl::download($goodstemp['itemDetail']['picUrl1']);
        }
        if(HttpCurl::download($goodstemp['itemDetail']['picUrl2'])){
            $goods->goods_pics .= ','.HttpCurl::download($goodstemp['itemDetail']['picUrl2']);
        }
        if(HttpCurl::download($goodstemp['itemDetail']['picUrl3'])){
            $goods->goods_pics .= ','.HttpCurl::download($goodstemp['itemDetail']['picUrl3']);
        }
        if(HttpCurl::download($goodstemp['itemDetail']['picUrl4'])){
            $goods->goods_pics .= ','.HttpCurl::download($goodstemp['itemDetail']['picUrl4']);
        }
        //$goods->goods_pics = HttpCurl::download($goodstemp['itemDetail']['picUrl1']).",".HttpCurl::download($goodstemp['itemDetail']['picUrl2']).",".HttpCurl::download($goodstemp['itemDetail']['picUrl3']).",".HttpCurl::download($goodstemp['itemDetail']['picUrl4']);
        $result1 = $goods->save();
        $result2 = true;
        $result3 = true;

        if(isset($goodstemp['categoryPathList']) && $goodstemp['categoryPathList']){
            foreach($goodstemp['categoryPathList'] AS $value){
                if(isset($value[0])){
                    /*$category = Category::findOne(['category_name'=>$value[0]['name']])->toArray();
                    //$goodsCategory = GoodsCategory::findOne(['goods_id'=>$result1->goods_id,'category_id'=>$category['category_id']])
                    $goodsCategory = new GoodsCategory();
                    $goodsCategory->goods_id = $goods->goods_id;
                    $goodsCategory->first_category_id = $category['category_id'];
                    if(isset($value[1])){
                        $category = Category::find()->where(['category_name'=>$value[1]['name']])->asArray()->one();
                        if($category)
                            $goodsCategory->second_category_id = $category['category_id'];
                    }
                    $result2 &= $goodsCategory->save();*/

                    $category = Category::find()->where(['yx_category_id'=>$value[0]['id']])->asArray()->one();
                    $goodsCategory = new GoodsCategory();
                    $goodsCategory->goods_id = $goods->goods_id;
                    $goodsCategory->first_category_id = $category['category_id'];
                    if(isset($value[1])){
                        $subcategory = Category::find()->where(['yx_category_id'=>$value[1]['id']])->asArray()->one();
                        $goodsCategory->second_category_id = $subcategory['category_id'];
                    }
                    $result2 &= $goodsCategory->save();

                }
            }
        }

        foreach($goodstemp['skuList'] AS $v){
            $sku_spec = [];
            $goodsSku = new GoodsSku();
            $goodsSku->yx_sku_id = $v['id'];
            $goodsSku->goods_id = $goods->goods_id;
            $goodsSku->sku_price = $v['yanxuanPrice'];
            $goodsSku->yanxuan_price = $v['yanxuanPrice'];
            $goodsSku->channel_price = $v['channelPrice'];
            $goodsSku->sku_pic = HttpCurl::download($v['picUrl']);
            $goodsSku->display_string = $v['displayString'];
            foreach($v['itemSkuSpecValueList'] AS $kk=>$vv){
                $sku_spec[] = ['spec_name'=>$vv['skuSpec']['name'],'spec_value'=>$vv['skuSpecValue']['value']];
            }
            $goodsSku->sku_spec = json_encode($sku_spec);
            $sku_price[] = $goodsSku->sku_price;
            $yanxuan_price[] = $goodsSku->yanxuan_price;

            foreach($sku_stock AS $vvv){
                if($vvv['skuId'] ==$v['id'])
                    $goodsSku->inventory = $vvv['inventory'];
            }

            $result3 &= $goodsSku->save();
        }
        $goods_price = min($sku_price);
        $yanxuan_price = min($yanxuan_price);
        if($result1 && $result2 && $result3){
            $goods->goods_price = $goods_price;
            $goods->yanxuan_price = $yanxuan_price;
            $goods->save();
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }

    }

    public function isExitGoods($yx_goods_id){
        return Goods::find()->where(['yx_goods_id'=>$yx_goods_id])->asArray()->one();
    }
}
